package com.hue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hue.entity.EduCourse;
import com.hue.entity.EduTeacher;
import com.hue.entity.R;
import com.hue.entity.vo.TeacherQuery;
import com.hue.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 姚凤桥
 * @since 2021-06-08
 */
@RestController
@RequestMapping("/teacher")
@CrossOrigin
@Api(description = "讲师管理")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("/findAll")
    @ApiOperation("所有讲师列表")
    public R findAll() {
        List<EduTeacher> eduTeachers = eduTeacherService.list(null);
        return R.ok().data("eduTeachers", eduTeachers);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除讲师")
    public R delete(@ApiParam(required = true, value = "讲师ID", name = "id") @PathVariable("id") String id) {
        return eduTeacherService.removeById(id) ? R.ok() : R.error();
    }

    @GetMapping("/pageFind/{page}/{size}")
    public R pageFind(@PathVariable("page") int page, @PathVariable("size") int size) {
        //分页查询
        Page<EduTeacher> eduTeacherPage = new Page<>(page, size);
        //将数据封装到eduTeacherPage
        eduTeacherService.page(eduTeacherPage, null);
        return R.ok().data("eduTeacherPage", eduTeacherPage);
    }

    //条件查询带分页
    @PostMapping("/conditionAndPageFind/{page}/{size}")
    public R conditionAndPageFind(@RequestBody(required = false) TeacherQuery teacherQuery,
                                  @PathVariable("page") int page,
                                  @PathVariable("size") int size) {
        //分页查询
        Page<EduTeacher> eduTeacherPage = new Page<>(page, size);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(teacherQuery.getName())) {
            queryWrapper.like("name", teacherQuery.getName());
        }
        if (teacherQuery.getLevel() != null) {
            queryWrapper.eq("level", teacherQuery.getLevel());
        }
        if (!StringUtils.isEmpty(teacherQuery.getBegin())) {
            queryWrapper.ge("gmt_create", teacherQuery.getBegin());
        }
        if (!StringUtils.isEmpty(teacherQuery.getEnd())) {
            queryWrapper.le("gmt_create", teacherQuery.getEnd());
        }
        eduTeacherService.page(eduTeacherPage, queryWrapper);
        return R.ok().data("eduTeacherPage", eduTeacherPage);
    }

    //添加讲师
    @PostMapping("/save")
    public R save(@RequestBody EduTeacher eduTeacher) {
        return eduTeacherService.save(eduTeacher) ? R.ok() : R.error();
    }

    //根据id查询讲师
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("eduTeacher", eduTeacher);
    }

    //修改讲师
    @PostMapping("update")
    public R update(@RequestBody EduTeacher eduTeacher) {
        return eduTeacherService.updateById(eduTeacher) ? R.ok() : R.error();
    }

    /*
        查询前4条讲师,按照sort升序
     */
    @GetMapping("findBySort")
    public R findBySort(){
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        wrapper.last(" limit 4");
        List<EduTeacher> eduTeachers = eduTeacherService.list(wrapper);
        return R.ok().data("eduTeachers",eduTeachers);
    }

}

